package ejb;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entidades.TProfesional;

/**
 * Session Bean implementation class AdministrarHistoriaBean
 */
@Stateless
@LocalBean
public class AdministrarLoginBean {

    @PersistenceContext(name = "Smerald2")
    EntityManager em;

    @SuppressWarnings("unchecked")
    public TProfesional validarLogin(TProfesional profesional) {

        if (profesional.getUserName() != null
                && profesional.getUserName().trim().length() > 0
                && profesional.getDesPass() != null
                && profesional.getDesPass().trim().length() > 0) {

            List<TProfesional> result = (ArrayList<TProfesional>) em
                    .createQuery(
                            "select t from TProfesional t "
                            + " WHERE t.userName =:user AND t.desPass=:pass ")
                    .setParameter("user", profesional.getUserName())
                    .setParameter("pass", profesional.getDesPass())
                    .getResultList();

            if (result != null && !result.isEmpty()) {
                return result.get(0);
            }
        }
        return null;
    }

}
